package de.justphil.tcg.tcgserver.game.mechanics.game;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import de.justphil.tcg.tcgserver.game.mechanics.monster.Monster;

public class Attack implements GameDataSerializable {

	private Monster attacker;
	private Monster target;

	public Attack() {
		
	}
	
	public Attack(Monster attacker, Monster target) {

		assert attacker != null && target != null : "Attacker and target must not be null!";

		this.attacker = attacker;
		this.target = target;
	}

	public Monster getAttacker() {
		return attacker;
	}

	public Monster getTarget() {
		return target;
	}

	/**
	 * Two Attacks equal each other when their attackers and targets are equal.
	 * 
	 */
	@Override
	public boolean equals(Object obj) {

		if (!Attack.class.isInstance(obj)) {

			return false;
		}
		Attack position = (Attack) obj;

		return position.attacker == this.attacker
				&& position.target == this.target;
	}

	/**
	 * int result = 17; result = 31 * result + attacker.hashCode(); result = 31
	 * result + target.hashCode(); return result;
	 */
	@Override
	public int hashCode() {

		int result = 17;
		result = 31 * result + attacker.hashCode();
		result = 31 * result + target.hashCode();
		return result;
	}

	@Override
	public String toString() {

		StringBuffer buffer = new StringBuffer();

		buffer.append('(');
		buffer.append(getAttacker());
		buffer.append(")=>(");
		buffer.append(getTarget());
		buffer.append(')');

		return new String(buffer);
	}

	@Override
	public void writeData(Game game, DataOutput out) throws IOException {
		game.getDataSerializableManager().writeMonster(attacker, game, out);
		game.getDataSerializableManager().writeMonster(target, game, out);
	}

	@Override
	public void readData(Game game, DataInput in) throws IOException {
		attacker = game.getDataSerializableManager().readMonster(game, in);
		target = game.getDataSerializableManager().readMonster(game, in);
	}
}
